/*
 * 在中间面板中显示文件夹下面的全部图片
 */
package com.propic.www;


import java.io.File;
import java.text.DecimalFormat;


import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.Tooltip;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class ShowFolderPic {
	public static boolean flagold = true;
	public static boolean flag = true;
	public static double width = 0;
	//在centerpane上显示图案
	public void showAllPic(String path) throws Exception{
		width = Mainwindows.centerpane.getWidth();
		if(!Mainwindows.flag){
			Mainwindows.centerpane.getChildren().clear();
			System.gc();
			Mainwindows.flag = true;
		}
		ScrollPane scrollPane = new ScrollPane();
		FlowPane pane = new FlowPane();
		pane.setMinWidth(Mainwindows.centerpane.getWidth());
		scrollPane.setContent(pane);
		Mainwindows.centerpane.getChildren().add(scrollPane);





		pane.setHgap(10);
		pane.setVgap(10);
		//设置边界大小
		int borderPic = 20;
		//设置图片显示大小
		double size = 150;
		Mainwindows.files = FilterPhoto.getPhoto(path);
		for (File f : Mainwindows.files) {



			Image image = new Image("file:"+f.getAbsolutePath());
			ImageView imageView = new ImageView(image);
			StackPane spaneOn = new StackPane();
			StackPane stackPane = new StackPane();

			//设置imageview的大小
			if(image.getHeight()>image.getWidth()){
				imageView.setFitHeight(size-borderPic);
				imageView.setFitWidth(((size-borderPic)/image.getHeight())*image.getWidth());
			}else {
				imageView.setFitHeight(((size-borderPic)/image.getWidth())*image.getHeight());
				imageView.setFitWidth(size-borderPic);
			}

			spaneOn.setMaxSize(imageView.getFitWidth()+7, imageView.getFitHeight()+7);
			spaneOn.setMinSize(imageView.getFitWidth()+7, imageView.getFitHeight()+7);
			stackPane.setMaxSize(imageView.getFitWidth()+7, imageView.getFitHeight()+7);
			stackPane.setMinSize(imageView.getFitWidth()+7, imageView.getFitHeight()+7);

//			imageView
			Tooltip tooltip = new Tooltip();
			tooltip.setText(f.getName());



			imageView.setOnMouseEntered(t->{
				stackPane.setStyle("-fx-background-color:#70dbe8;");
				scrollPane.setTooltip(tooltip);

			});

			imageView.setOnMouseExited(t->{
				stackPane.setStyle("");
			});
			
			//设置动作
			imageView.setOnMouseClicked((MouseEvent t) ->{
				if(Mainwindows.copyfile.isEmpty()){
					Mainwindows.Img_delete.setVisible(false);

				}else {
					Mainwindows.Img_delete.setVisible(true);
				}
				MouseButton button = t.getButton();
				switch (button) {
					case NONE:
						break;
					case PRIMARY:
					if(t.getClickCount()==1){
						double n = 0;
						ImageView yesview = new ImageView(new Image(String.valueOf(getClass().getResource("res/yes_green.png"))));
						if (imageView.getFitWidth()>imageView.getFitWidth()){
							n=imageView.getFitWidth();
						}else {
							n=imageView.getFitHeight();
						}
						yesview.setFitHeight(n*0.6);
						yesview.setFitWidth(n*0.6);
						String name = f.getAbsolutePath();
						if(Mainwindows.copyfile.contains(name)){
							System.out.println("取消选择");
							spaneOn.getChildren().clear();
							spaneOn.getChildren().add(imageView);
							Mainwindows.copyfile.remove(name);
							spaneOn.setStyle("");
						}else {
							System.out.println("点击选择");
							spaneOn.setStyle("-fx-background-color:#70dbe8;");
							spaneOn.getChildren().add(yesview);
							Mainwindows.copyfile.add(name);
						}

						
					}else if(t.getClickCount()==2){

					}
					break;
				case SECONDARY:
					System.out.println("右键");
					if(t.getClickCount()==1 ){
						Mainwindows.cmRight.show(imageView, t.getScreenX(), t.getScreenY());
						
						/*绑定openImage(打开一个图片)的动作,由于调用OpensinglePhoto出错，
						 * 所以不再调用OpensinglePhoto中的函数，等待修复
						 * */
						Mainwindows.openImage.setOnAction((ActionEvent e)->{
							System.out.println(Mainwindows.files.indexOf(f));
							Stage stagetest = new Showpane().showpic(f);
							stagetest.show();
						});

						//绑定Img_attriBute（图片属性）的动作
						Mainwindows.Img_attriBute.setOnAction(e->{
							ImageView imgV = (ImageView) t.getSource();
							Image img = imgV.getImage();
//							String name = Mainwindows.map.get(n);
							String name = f.getAbsolutePath();
							File filetmp = new File(name);
							Stage stage = new Stage();
							GridPane cenPane = new GridPane();
							double length = img.getWidth();
							double height = image.getHeight();
							Text textname = new Text(" 文件名 : "+filetmp.getName()+"   ");
							Text textpath = new Text("文件目录: "+filetmp.getParent()+"  ");
							long filelength = filetmp.length();
							double length_human = filelength/1024.0;
							DecimalFormat df = new DecimalFormat("#.00");
							df.format(length_human);
							Text textsize = new Text("图片尺寸: "+new String((int)length+"*"+(int)height));
							Text texthowbigbig = new Text("文件大小: "+df.format(length_human)+"KB"+"("+filelength+"bytes)");
							cenPane.setOnMouseClicked(click->{
								if(click.getClickCount() == 1){
									stage.close();
								}
							});

							cenPane.setHgap(5);
							cenPane.setVgap(5);
							cenPane.setPadding(new Insets(40, 0, 10, 50));
							cenPane.add(textname, 0, 1);
							cenPane.add(textpath, 0, 2);
							cenPane.add(textsize, 0, 3);
							cenPane.add(texthowbigbig,0,4);
							cenPane.setStyle("-fx-background-color:ffffff");
							StackPane pane1 = new StackPane();
							pane1.getChildren().add(cenPane);
							cenPane.setMinSize(480, 280);
							cenPane.setMaxSize(480, 280);
							pane1.setStyle("-fx-background-color:#a4e6ea");
							Scene scene = new Scene(pane1,500, 300);
							stage.setScene(scene);
							stage.initStyle(StageStyle.TRANSPARENT);
							stage.show();
						});

						//绑定Img_delete（删除图片）的动作
						Mainwindows.Img_delete.setOnAction(delete->{

							if (!Mainwindows.copyfile.isEmpty()){
								for(String deletefilepath : Mainwindows.copyfile){
									File filetmp = new File(deletefilepath);
									filetmp.delete();
								}
							}
							Mainwindows.centerpane.getChildren().clear();
							try {
								showAllPic(path);
							} catch (Exception e) {
								e.printStackTrace();
							}
						});

						Mainwindows.renameImage.setOnAction(rename->{

							Stage stagerename = new RenamePane().rename();
							stagerename.show();

						});
					}
					break;
				case MIDDLE:
					System.out.println("中键");
					break;
				default:
					break;
				}

					
			});
//            Mainwindows.
            Mainwindows.Mainpane.setOnMouseMoved(t->{
                if (!(width-Mainwindows.centerpane.getWidth()<1 && width-Mainwindows.centerpane.getWidth()>-1)){
                    scrollPane.setMaxWidth(Mainwindows.centerpane.getWidth());
                    scrollPane.setMinWidth(Mainwindows.centerpane.getWidth());
                    pane.setMaxWidth(Mainwindows.centerpane.getWidth());
                    pane.setMinWidth(Mainwindows.centerpane.getWidth());
                    width = Mainwindows.centerpane.getWidth();
                    System.out.println("调整大小");
                }
            });

			System.out.println(f.getName());
			spaneOn.getChildren().add(imageView);
			stackPane.getChildren().add(spaneOn);
			pane.getChildren().add(stackPane);
			
		}


	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
